using System;
namespace 面向对象
{
    class Program
    {//形状抽象类（父类）
        abstract class Shape
        {
            public abstract double Area();         //面积           
            public abstract double Perimeter(); //周长        
            public abstract void Initialization();//初始化数据
        }
        class Circle : Shape
        {
            double r;
            public Circle()
            {
                Initialization();
            }

            public override double Area()
            {
                return Math.PI * Math.Pow(r, 2);
            }

            public override double Perimeter()
            {
                return 2 * Math.PI * r;
            }

            public override void Initialization()
            {
                while (true)
                {
                    Console.WriteLine("请输入圆的半径：");
                    string r_1 = Console.ReadLine();
                    if (!double.TryParse(r_1, out r))
                    {
                        Console.WriteLine("输入错误");
                        continue;
                    }
                    break;
                }
            }


        }


        //长方形
        class Rectangle : Shape
        {

            double width;
            double height;
            public Rectangle()
            {
                Initialization();
            }

            public override double Area()
            {
                return width * height;
            }

            public override double Perimeter()
            {
                return 2 * (width + height);
            }

            public override void Initialization()
            {while (true)
                {
                    Console.WriteLine("请选择输入长方形的长：");
                    string width_1 = Console.ReadLine();
                    Console.WriteLine("请选择输入长方形的宽：");
                    string height_1 = Console.ReadLine();
                    if(!double.TryParse(width_1, out width)||!double.TryParse(height_1, out height))
                    {
                        Console.WriteLine("输入错误");
                        continue;
                    }
                    break;
                }
            }
        }

        //正方形
        class Square : Shape
        {
            double side;
            public Square()
            {
                Initialization();
            }
            public override double Area()
            {
                return Math.Pow(side, 2);
            }

            public override double Perimeter()
            {
                return 4 * side;
            }

            public override void Initialization()
            {
                while (true)
                {
                    Console.WriteLine("请输入正方形的边长：");
                    string side_1 = Console.ReadLine();
                    if (!double.TryParse(side_1, out side))
                    {
                        Console.WriteLine("输入错误");
                        continue;
                    }
                    break;
                   
                }
            }
        }

        //三角形
        class Triangle : Shape
        {
            double side1;
            double side2;
            double side3;
            public Triangle()
            {

                Initialization();
            }

            public override double Area()
            {

                return Math.Sqrt((side1 + side2 + side3) * (side1 + side2 - side3) * (side1 + side3 - side2) * (side2 + side3 - side1)) / 4;
            }

            public override double Perimeter()
            {

                return side1 + side2 + side3;
            }

            public override void Initialization()
            {while (true)
                {
                    Console.Write("请输入三角形第1个边长：");
                    string side1Str = Console.ReadLine();
                    Console.Write("请输入三角形第2个边长：");
                    string side2Str = Console.ReadLine();
                    Console.Write("请输入三角形第3个边长：");
                    string side3Str = Console.ReadLine();
                   if( !double.TryParse(side1Str, out side1)||
                    !double.TryParse(side2Str, out side2)||
                    !double.TryParse(side3Str, out side3)||
                    side1 >=side2+side3|| side2 >= side1 + side3 || side3 >= side1 + side2)
                    {
                        Console.WriteLine("输入错误");
                        continue;
                    }
                    break;
                }
            }
        }

        class ShapeAll
        {

            public static Shape Createshape(string name)
            {

                switch (name)
                {
                    case "1": return new Circle();
                    case "2": return new Rectangle();
                    case "3": return new Square();
                    case "4": return new Triangle();
                    default:
                        return null;
                }
            }
        }


        static void Main(string[] args)
        {
            Console.WriteLine("请选择您要计算的形状：");
            Console.WriteLine("1、圆 2、正方形 3、长方形 4、三角形");
            string name = Console.ReadLine();
            Shape shape = ShapeAll.Createshape(name);
            if (shape != null)
            {
                Console.WriteLine("面积为：{0}\r\n周长为：{1}", shape.Area(), shape.Perimeter());
            }
            else Console.Write("输入有误！");
        }
    }
}